/**
 * 全局共享静态信息
 */
// 动态获取状态
import { get_system, get_wan, get_lan, get_perm, } from '@/api/global'
import perm_map from "@/perm_map.js"
const getState = ()=> {
  return {
    lan_conf: "",
    wan_conf: "",
    system_status: '',
    // 二级菜单配置信息
    perm: '',
    // 二级菜单的控制文件
    perm_map,
    // 控制菜单的版本 oldmenu or newmenu
    version: "oldmenu",
    durationReboot: 30000,
    durationReset: 6000,
    durationLong: 180000, // 3分钟
    durationMedium: 90000, // 1.5分钟
    showModal: false,
    flushed: false,
    successMsg: "", // 长时间断网操作成功后的提示信息
  }
}

// 函数化 解耦
const state = getState()

const mutations = {
  // 函数化 刷新数据 重新执行函数
  reset_state: state => {
    Object.assign(state, getState())
  },
  set_system: (state, data)=> {
    state.system_status = data
  },
  set_lan: (state, data)=> {
    state.lan_conf = data
  },
  set_wan: (state, data)=> {
    state.wan_conf = data
  },
  set_perm: (state, data) => {
    state.perm = data
  },
  set_permMap: (state, data) => {
    state.perm_map = data
  },
  set_modal: (state, data) => {
    state.showModal = data
  },
  set_flush: (state, data) => {
    state.flushed = data
  },
  set_successMsg: (state, data) => {
    state.successMsg = data
  }
}
const actions = {
  // mutations 接收参数为 $store对象
  get_system: ({commit}) => {
    return new Promise((resolve, reject) => {
      get_system().then(res => {
        commit('set_system', res.data)
        resolve()
      }).catch( err => {
        reject(err)
      })
    })
  },
  get_wan: ({commit}) => {
    return new Promise((resolve, reject) => {
      get_wan().then(res => {
        commit('set_wan', res.data)
        resolve()
      }).catch( err => {
        reject(err)
      })
    })
  },
  get_lan: ({commit}) => {
    return new Promise((resolve, reject) => {
      get_lan().then(res => {
        commit('set_lan', res.data)
        resolve()
      }).catch( err => {
        reject(err)
      })
    })
  },
  // get_perm({commit, dispatch}){ }  是函数声明 与 函数表达式 函数内能访问函数自身
  // get_perm: ({commit, dispatch})=> { }  是键值对 函数内不能访问key key是一个字符串 key不能被直接引用
  get_perm: ({commit}) => {
    // 注意return Promise的层次  内部处理 依赖 回调(栈)的层次关系
    return new Promise((resolve, reject) => {
      get_perm().then(res=> {
        commit("set_perm", res)
        console.log("get_perm")
        resolve()
      }).catch(err => {
        reject(err)
      })
    })
  },
  // get_permMap: ({commit, state}) => {
  //   return new Promise((resolve, reject) => {
  //     commit("set_permMap", perm_map)
  //     console.log(state.perm_map)
  //     resolve()
  //   })
  // },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
}
